package com.yiyi.controller;

import com.yiyi.form.AddAnswerForm;
import com.yiyi.form.SearchQuestionForm;
import com.yiyi.service.AnswerService;
import com.yiyi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author yy
 * @since 2021-05-29
 *
 * 1、新增题目
 * 2、删除题目
 * 3、对全部题目进行分页查询
 * 4、按公司id查询题目列表
 * 5、按题目id查询单道题目
 * 6、按题目id列表查询多道题目
 */
@RestController
@RequestMapping("/answer")
@Api(tags = {"简答题操作接口"})
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    /**
     * 新增题目
     *
     * @param answerForm
     * @return
     * */
    @ApiOperation(value = "新增题目")
    @PostMapping("/save")
    public R save(AddAnswerForm answerForm) {
        int flag = answerService.save(answerForm);
        if (flag != 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除题目
     *
     * @param id
     * @return
     * */
    @ApiOperation(value = "删除题目")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        int flag = answerService.delete(id);
        if (flag != 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 对题目进行分页查询及搜索
     *
     * @param searchQuestionForm
     * @return
     * */
    @ApiOperation(value = "分页查询及搜索题目")
    @PostMapping("/findByPage")
    public R findByPage(@RequestBody SearchQuestionForm searchQuestionForm) {
        return R.ok(answerService.findByPage(searchQuestionForm));
    }

    /**
     * 按公司id列表查询多道题目
     *
     * @param companyId
     * @return
     * */
    @ApiOperation(value = "按公司id列表查询多道题目")
    @GetMapping("/findByCompanyId/{companyId}")
    public R findByCompanyId(@PathVariable("companyId") String companyId) {
        return R.ok(answerService.findByCompanyId(companyId));
    }

    /**
     * 按题目id查询单道题目
     *
     * @param id
     * @return
     * */
    @ApiOperation(value = "按题目id查询单道题目")
    @GetMapping("/findOneById/{id}")
    public R findOneById(@PathVariable("id") String id) {
        return R.ok(answerService.findOneById(id));
    }

    /**
     * 按题目id列表查询多道题目
     *
     * @param ids
     * @return
     * */
    @ApiOperation(value = "按题目id列表查询多道题目")
    @PostMapping("/findListByIds")
    public R findListByIds(@RequestBody List<String> ids) {
        return R.ok(answerService.findListByIds(ids));
    }
}

